<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          :router="true"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <!-- 一级菜单 -->
          <!-- 1. 用户管理 -->
          <el-submenu
            :index="menu.id + ''"
            v-for="menu in menuList"
            :key="menu.id"
          >
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <i class="iconfont" :class="'icon-' + iconsObj[menu.id]"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in menu.children"
              :key="item.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuList: [],
      iconsObj: {
        125: 'users',
        103: 'tijikongjian',
        101: 'shangpin',
        102: 'danju',
        145: 'baobiao',
        110: '',
        111: '',
        112: '',
        104: '',
        115: '',
        121: '',
        107: '',
        146: ''
      }
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      padding-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .toggle-button {
    text-align: center;
    color: #fff;
    padding: 2px 0;
    letter-spacing: 2px;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
    .iconfont {
      margin-right: 10px;
    }
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>
